package com.yijiu.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yijiu.wiki.domain.Ebook;
import com.yijiu.wiki.domain.EbookExample;
import com.yijiu.wiki.mapper.EbookMapper;
import com.yijiu.wiki.req.EbookQueryReq;
import com.yijiu.wiki.req.EbookSaveReq;
import com.yijiu.wiki.resp.EbookQueryResp;
import com.yijiu.wiki.resp.PageResp;
import com.yijiu.wiki.util.CopyUtil;
import com.yijiu.wiki.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ligaojian
 * @version 1.0
 * @description: TODO
 * @date 2021/10/30 9:47
 */
@Service
public class EbookService {

    @Resource
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;

    public PageResp<EbookQueryResp> list(EbookQueryReq req){

        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        System.out.println();
        //修改动态sql
        if (!ObjectUtils.isEmpty(req.getName())){
            criteria.andNameLike("%"+req.getName()+"%");
        }
//        if (!ObjectUtils.isEmpty(req.getCategoryId2())){
//            criteria.andCategory2IdEqualTo(req.getCategoryId2());
//        }
        //使用pageHelper插件实现分页
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);

        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
//        pageInfo.getTotal();
//        pageInfo.getPages();
//        List<EbookResp> respList = new ArrayList<>();
//        for (Ebook ebook : ebookList) {
////            EbookResp ebookResp = new EbookResp();
////            BeanUtils.copyProperties(ebook,ebookResp); //对象的复制
//
//            EbookResp ebookResp = CopyUtil.copy(ebook, EbookResp.class);
//            respList.add(ebookResp);
//        }

        List<EbookQueryResp> list = CopyUtil.copyList(ebookList, EbookQueryResp.class);
        PageResp<EbookQueryResp> pageResp = new PageResp();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);

        return pageResp;
    }

    /**
    * @description: 编辑电子书中的保存
    * @author ligaojian
    * @date
    * @version 1.0
    */
    public void save(EbookSaveReq req) {
        Ebook ebook = CopyUtil.copy(req,Ebook.class);
        if (ObjectUtils.isEmpty(req.getId()) || req.getId()==0){
            //新增,使用雪花算法生成id
//            ebook.setId(String.valueOf(snowFlake.nextId()));
            ebook.setId(snowFlake.nextId());
            System.out.println("新增数据的id"+ebook.getId());
            ebookMapper.insert(ebook);
        }else {
            ebookMapper.updateByPrimaryKey(ebook);
        }
    }

    /**
    * @description: 删除电子书
    * @author
    * @date
    * @version 1.0
    */
    public void delete(Long id) {
        ebookMapper.deleteByPrimaryKey(id);
        System.out.println("删除成功！");
    }
}
